<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>Gogs中文文档 - 书栈(BookStack.CN)</title>
    <link href="editormd.css" rel="stylesheet">
</head>
<body>
    <h1 id="article-title">2.1 注册 Windows 服务</h1>
    <div class="article-body markdown-body editormd-preview-container"  id="page-content">
        <div class="markdown-toc editormd-markdown-toc"><ul class="markdown-toc-list"><li><a class="toc-level-1" href="#注册为 Windows 服务运行" level="1">注册为 Windows 服务运行</a><ul><li><a class="toc-level-3" href="#前提要求" level="3">前提要求</a></li><li><a class="toc-level-3" href="#Use Builtin Functionality" level="3">Use Builtin Functionality</a></li><li><a class="toc-level-3" href="#Use NSSM" level="3">Use NSSM</a><ul></ul></li></ul></li></ul></div><h1 id="h1--windows-"><a name="注册为 Windows 服务运行" class="reference-link"></a><span class="header-link octicon octicon-link"></span>注册为 Windows 服务运行</h1><h3 id="h3-u524Du63D0u8981u6C42"><a name="前提要求" class="reference-link"></a><span class="header-link octicon octicon-link"></span>前提要求</h3><p>想要使 Gogs 通过 Windows 服务的方式运行，必须满足以下两个条件：</p>
<ol>
<li>使用 <code>miniwinsvc</code> 构建标签获得内置 Windows 服务支持。</li><li>不使用 <code>miniwinsvc</code> 构建标签并通过 <a href="http://nssm.cc/">NSSM</a> 注册为服务。</li></ol>
<p>在注册成为服务之前，需要确保给予 Gogs 二进制相应目录的读写权限，包括存放仓库的根目录（<code>[repository] ROOT</code>）。</p>
<p>修改 <code>C:\Gogs\custom\conf\app.ini</code> 文件的相应信息：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">RUN_USER </span><span class="pun">=</span><span class="pln"> COMPUTERNAME$</span></code></li></ol></pre><p>通过上面的配置将 Gogs 的运行用户设置为本地系统用户。<code>COMPUTERNAME</code> 的值可以通过命令 <code>echo %COMPUTERNAME%</code> 获得，如果该命令的返回值为 <code>USER-PC</code> 则使用 <code>RUN_USER = USER-PC$</code>：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pun">[</span><span class="pln">server</span><span class="pun">]</span></code></li><li class="L1"><code><span class="pln">DOMAIN </span><span class="pun">=</span><span class="pln"> gogs</span></code></li><li class="L2"><code><span class="pln">PROTOCOL </span><span class="pun">=</span><span class="pln"> http</span></code></li><li class="L3"><code><span class="pln">HTTP_ADDR </span><span class="pun">=</span><span class="pln"> </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">1.1</span></code></li><li class="L4"><code><span class="pln">HTTP_PORT </span><span class="pun">=</span><span class="pln"> </span><span class="lit">80</span></code></li><li class="L5"><code><span class="pln">OFFLINE_MODE </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span></code></li><li class="L6"><code><span class="pln">ROOT_URL </span><span class="pun">=</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//gogs/</span></code></li></ol></pre><p>Moves Gogs’ listen port to 80 on the local interface subnet, and tells the Gogs HTTPd that its virtual host is the “gogs” domain.<br/>This lets you forgo including a port number when browsing, and prevents collision with other localhost services.<br/>The IP address can be anything in the range <code>127.0.0.2 - 127.254.254.254</code>, so long as it’s unique to Gogs.</p>
<p>To complete that network route, open Notepad.exe as administrator and include the following in <code>C:\Windows\System32\drivers\etc\hosts</code>:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="com"># Gogs local HTTPd</span></code></li><li class="L1"><code><span class="lit">127.0</span><span class="pun">.</span><span class="lit">1.1</span><span class="pln">        gogs</span></code></li></ol></pre><p>The hosts file entry will guarantee that all requests to the “gogs” domain are captured by your localhost interface.<br/>In a web browser, generally, <code>gogs/</code> in the address bar is sufficient to trigger that route.</p>
<h3 id="h3-use-builtin-functionality"><a name="Use Builtin Functionality" class="reference-link"></a><span class="header-link octicon octicon-link"></span>Use Builtin Functionality</h3><p>Open a command prompt (<code>cmd.exe</code>) as an Administrator. Run the following command:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt; sc create gogs start</span><span class="pun">=</span><span class="pln"> </span><span class="kwd">auto</span><span class="pln"> binPath</span><span class="pun">=</span><span class="pln"> </span><span class="str">&#34;\&#34;C:\gogs\gogs.exe\&#34; web --config \&#34;C:\gogs\custom\conf\app.ini\&#34;&#34;</span></code></li></ol></pre><p>Ensure there is a space after each <code>=</code>. You can choose to add additional Arguments to further modify your service, or modify it manually in the service management console.</p>
<p>To start the service run following command:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt; net start gogs</span></code></li></ol></pre><p>You should see following output:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="typ">The</span><span class="pln"> gogs service </span><span class="kwd">is</span><span class="pln"> starting</span><span class="pun">.</span></code></li><li class="L1"><code><span class="typ">The</span><span class="pln"> gogs service was started successfully</span><span class="pun">.</span></code></li></ol></pre><h3 id="h3-use-nssm"><a name="Use NSSM" class="reference-link"></a><span class="header-link octicon octicon-link"></span>Use NSSM</h3><p>Get the <a href="http://nssm.cc/download">nssm.exe</a> needed for your machine (32 or 64 bit; they’re packaged together in Iain’s zip file), and place it in a directory that is in (or will be added to) your <code>%PATH%</code> environment variable.</p>
<p>Open a command line as administrator and do following command to configure Gogs as a service:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt;nssm install gogs</span></code></li></ol></pre><p>“NSSM service installer” will appear. Configure it as follows:</p>
<p>Application tab:</p>
<ul>
<li>Path: <code>C:\gogs\gogs.exe</code></li><li>Startup directory: <code>C:\gogs</code></li><li>Arguments: <code>web</code></li></ul>
<p><img src="8b35028d0518c77d8308ff3dd97e97a0.png" alt="install_gogs_on_windows_nssm_1.png" title="install_gogs_on_windows_nssm_1.png"/></p>
<p>Details tab:</p>
<ul>
<li>Display name: <code>Gogs</code></li><li>Description: <code>A painless self-hosted Git service.</code></li><li>Startup type: <code>Automatic (Delayed Start)</code></li></ul>
<p>Note that we’ve chosen <a href="http://stackoverflow.com/a/11015576">delayed start</a>, so that the service will not impact the early boot time. Gogs will start two minutes after the non-delayed services.</p>
<p><img src="e00b4e8899e00091e6ac46d230d53c2f.png" alt="install_gogs_on_windows_nssm_2.png" title="install_gogs_on_windows_nssm_2.png"/></p>
<p>I/O tab:</p>
<ul>
<li>Output (stdout): <code>C:\gogs\log\gogs-nssm.txt</code></li><li>Error (stderr): <code>C:\gogs\log\gogs-nssm.txt</code></li></ul>
<p>That will capture all text output that you would normally receive from Gogs on the command line console, and log it to that file instead.</p>
<p><img src="d92fc8f1c8954dda582909a8d37bc6c6.png" alt="install_gogs_on_windows_nssm_3.png" title="install_gogs_on_windows_nssm_3.png"/></p>
<p>File rotation tab:</p>
<ul>
<li>Check: <code>Rotate files</code></li><li>Restrict rotation to files bigger than: <code>1000000 bytes</code></li></ul>
<p><img src="a16e019f33ab5660b83b52f2ee5c8a24.png" alt="install_gogs_on_windows_nssm_4.png" title="install_gogs_on_windows_nssm_4.png"/></p>
<p>Environment tab:</p>
<ul>
<li>Environment variables: <code>PATH=%PATH%;C:\gogs;C:\Program Files (x86)\Git\bin</code></li></ul>
<p>That is a guarantee that both <code>gogs.exe</code> and <code>git.exe</code> will be on the Gogs service’s path variable during runtime.</p>
<p><img src="d4643fd6157facd3f4178141fb4ab85d.png" alt="install_gogs_on_windows_nssm_5.png" title="install_gogs_on_windows_nssm_5.png"/></p>
<p>Click “Install service”, and you should be confirmed that it succeeded.</p>
<p>If it failed, refer back to the command line console you started, for the error message. When it succeeds, go to command line and do:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">nssm start gogs</span></code></li></ol></pre><p>You should see:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> START</span><span class="pun">:</span><span class="pln"> </span><span class="typ">The</span><span class="pln"> operation completed successfully</span><span class="pun">.</span></code></li></ol></pre><p>Check that this is true by opening <code>C:\gogs\log\gogs-nssm.txt</code>. You should see Gogs’ start up procedures, ending with:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">timestamp </span><span class="pun">[</span><span class="pln">I</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Run</span><span class="pln"> </span><span class="typ">Mode</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Production</span></code></li><li class="L1"><code><span class="pln">timestamp </span><span class="pun">[</span><span class="pln">I</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Listen</span><span class="pun">:</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//127.0.1.1:80</span></code></li></ol></pre><p>Now point your web browser at <code>http://gogs/</code> and log in.</p>
<p>Gogs is running as a service, and will not need to be run manually, unless something goes wrong. NSSM will attempt to restart the service for you, if the Gogs server crashes.</p>
<p>When you need to restart it after <code>app.ini</code> changes, go to an administrator command line after the changes, and do:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">nssm restart gogs</span></code></li></ol></pre><p>See the <a href="/docs/advanced/configuration_cheat_sheet.html">configuration cheat sheet</a> for reference of the <code>custom\conf\app.ini</code> settings.</p>
<p>An example of the logging and error handling in action:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pun">[</span><span class="pln">log</span><span class="pun">]</span></code></li><li class="L1"><code><span class="pln">ROOT_PATH </span><span class="pun">=</span><span class="pln"> C</span><span class="pun">:</span><span class="pln">\gogs\log</span></code></li></ol></pre><p>At the time of writing this line, it will cause the following to print in <code>C:\gogs\log\gogs-nssm.txt</code>:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">timestamp </span><span class="pun">[</span><span class="pln">T</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Custom</span><span class="pln"> path</span><span class="pun">:</span><span class="pln"> C</span><span class="pun">:</span><span class="str">/gogs/</span><span class="pln">custom</span></code></li><li class="L1"><code><span class="pln">timestamp </span><span class="pun">[</span><span class="pln">T</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Log</span><span class="pln"> path</span><span class="pun">:</span><span class="pln"> C</span><span class="pun">:</span><span class="pln">\gogs\log</span></code></li><li class="L2"><code><span class="pln">timestamp </span><span class="pun">[</span><span class="pln">I</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Gogs</span><span class="pln"> x</span><span class="pun">.</span><span class="pln">y</span><span class="pun">.</span><span class="pln">z</span></code></li><li class="L3"><code><span class="pln">timestamp </span><span class="pun">[</span><span class="pln">log</span><span class="pun">.</span><span class="pln">go</span><span class="pun">:</span><span class="lit">294</span><span class="pln"> </span><span class="typ">Error</span><span class="pun">()]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">E</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Fail</span><span class="pln"> to </span><span class="kwd">set</span><span class="pln"> logger</span><span class="pun">(</span><span class="pln">file</span><span class="pun">):</span><span class="pln"> invalid character </span><span class="str">&#39;g&#39;</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> escape code</span></code></li></ol></pre><p>This is what was expected in <code>C:\custom\conf\app.ini</code>:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">ROOT_PATH </span><span class="pun">=</span><span class="pln"> C</span><span class="pun">:</span><span class="str">/gogs/</span><span class="pln">log</span></code></li></ol></pre><p>And this was the <code>nssm</code> interaction while solving it:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt;nssm restart gogs</span></code></li><li class="L1"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> STOP</span><span class="pun">:</span><span class="pln"> </span><span class="typ">The</span><span class="pln"> operation completed successfully</span><span class="pun">.</span></code></li><li class="L2"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Unexpected</span><span class="pln"> status SERVICE_PAUSED </span><span class="kwd">in</span><span class="pln"> response to START control</span><span class="pun">.</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt;nssm start gogs</span></code></li><li class="L5"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> START</span><span class="pun">:</span><span class="pln"> </span><span class="typ">An</span><span class="pln"> instance of the service </span><span class="kwd">is</span><span class="pln"> already running</span><span class="pun">.</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt;nssm stop gogs</span></code></li><li class="L8"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> STOP</span><span class="pun">:</span><span class="pln"> </span><span class="typ">The</span><span class="pln"> operation completed successfully</span><span class="pun">.</span></code></li><li class="L9"><code></code></li><li class="L0"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt;nssm start gogs</span></code></li><li class="L1"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Unexpected</span><span class="pln"> status SERVICE_PAUSED </span><span class="kwd">in</span><span class="pln"> response to START control</span><span class="pun">.</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pln">C</span><span class="pun">:</span><span class="pln">\&gt;nssm restart gogs</span></code></li><li class="L4"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> STOP</span><span class="pun">:</span><span class="pln"> </span><span class="typ">The</span><span class="pln"> operation completed successfully</span><span class="pun">.</span></code></li><li class="L5"><code><span class="pln">gogs</span><span class="pun">:</span><span class="pln"> START</span><span class="pun">:</span><span class="pln"> </span><span class="typ">The</span><span class="pln"> operation completed successfully</span><span class="pun">.</span></code></li></ol></pre>
    </div>
</body>
</html>